﻿var LeftMenu = function(container) {
    var self = this;
    var visible = false;
    var Elements = {
        overlay: $("#leftMenuOverlay"),
        detaliiAngajat: container.find(".detaliiAngajat"),
        pozaAngajat: container.find(".detaliiAngajat img"),
        numeAngajat: container.find(".detaliiAngajat .meniu-profile-name"),
        panouControl: container.find(".panouControl"),
        cereriConcediu: container.find(".cereriConcediu"),
        listaAngajati: container.find(".listaAngajati"),
        notificari: container.find(".notificariInfo"),
        numarNotificari: container.find(".numarNotificari"),
        setari: container.find(".setari"),
        detaliiFirma: container.find('.detaliiFirma'),
        pozaFirma: container.find(".detaliiFirma img"),
        numeFirma: container.find(".detaliiFirma .meniu-company-name"),
        deconectare: container.find(".deconectare")
    };

    var gotNotifications = false;

    this.IsGoing = false;

    var getNameHtml = function(name) {
        var n = name.split(' ');
        var html = "";
        for (var i = 0; i < n.length; i++) {
            html += n[i];
            if (i < n.length - 1)
                html += "<br/>";
        }
        return html;
    };

    var updateUserPicture = function() {
        app.TMMService.TMM.GetUserInfo(function(response) {
            if (response && response.FinalResult) {
                var user = response.FinalResult;
                Elements.pozaAngajat.attr("src", Utils.getEmployeePhoto(user.HasPicture ? (user.PRS_ID ? user.PRS_ID : user.Username) : 0));
            }
        });
    };

    var updateCompany = function() {
        Elements.pozaFirma.attr("src", app.User.CompanyPicUrl || getImageUrl(Constants.defaultCompanyPic));
        Elements.pozaFirma.attr('alt', app.User.Company);
        Elements.numeFirma.html(getNameHtml(app.User.Company));
    };

    var setListaAngajatiMenu = function() {
        if (app.User.IsOnlyEmployee)
            Elements.listaAngajati.hide();
        else Elements.listaAngajati.show();
    };

    var setCereriConcediiMenu = function() {
        if (app.User.Rights.ALL || app.User.Rights.AccesModulCereriConcedii)
            Elements.cereriConcediu.hide();
        else Elements.cereriConcediu.show();
    };

    var initialize = function() {
        updateUserPicture();

        var nume = "";
        if (app.User.PRS_ID) {
            nume = app.User.NumeAngajat;
            Elements.pozaAngajat.attr('alt', app.User.NumeAngajat);
        } else {
            nume = app.User.Username;
            Elements.pozaAngajat.attr('alt', app.User.Username);
        }

        var nameHtml = getNameHtml(nume);

        Elements.numeAngajat.html(nameHtml);

        updateCompany();

        Elements.detaliiAngajat.find('.main-meniu-link').html(translate('Module_Title_EmployeeInfo'));
        Elements.detaliiAngajat.unbind().tap(function() {
            self.hide();
            if (app.CurrentPage.container.selector == app.Pages.MyProfile.container.selector)
                return history.back();
            if (!app.Pages.MyProfile.instance)
                app.Pages.MyProfile.instance = new MyProfilePage(app.Pages.MyProfile.container);
            app.changePage(app.Pages.MyProfile, false, true);
        });

        Elements.panouControl.find('.main-meniu-link').html(translate('Module_Title_Dashboard'));
        Elements.panouControl.find("a:first").unbind().tap(function() {
            self.hide();
            if (app.CurrentPage.container.selector == app.Pages.Dashboard.container.selector)
                return history.back();
            if (!app.Pages.Dashboard.instance)
                app.Pages.Dashboard.instance = new Dashboard(app.Pages.Dashboard.container);
            app.changePage(app.Pages.Dashboard, false, true);
        });

        Elements.cereriConcediu.find('.main-meniu-link').html(translate('Module_Title_CereriConcediiPage'));
        Elements.cereriConcediu.find("a:first").unbind().tap(function() {
            self.hide();
            if (app.CurrentPage.container.selector == app.Pages.CereriConcedii.container.selector)
                return history.back();
            if (!app.Pages.CereriConcedii.instance) {
                app.Pages.CereriConcedii.instance = new CereriConcediiPage(app.Pages.CereriConcedii.container);
                app.Pages.CereriConcedii.instance.initialize();
            }
            app.changePage(app.Pages.CereriConcedii, false, true);
            app.Pages.CereriConcedii.instance.resetFilter().refresh(true);
        });

        setCereriConcediiMenu();

        Elements.listaAngajati.find('.main-meniu-link').html(translate('Module_Title_EmployeeList'));
        Elements.listaAngajati.find("a:first").unbind().tap(function() {
            self.hide();
            if (app.CurrentPage.container.selector == app.Pages.EmployeesList.container.selector)
                return history.back();
            if (!app.Pages.EmployeesList.instance)
                app.Pages.EmployeesList.instance = new EmployeesListPage(app.Pages.EmployeesList.container);
            else app.Pages.EmployeesList.instance.refresh(true);

            app.changePage(app.Pages.EmployeesList, false, true);
        });

        setListaAngajatiMenu();

        Elements.notificari.find('.main-meniu-link span:first').html(translate('Module_Title_Notifications'));
        Elements.notificari.find("a:first").unbind().tap(function() {
            self.hide();
            if (app.CurrentPage.container.selector == app.Pages.Notifications.container.selector)
                return history.back();
            if (!app.Pages.Notifications.instance) {
                app.Pages.Notifications.instance = new NotificationsPage(app.Pages.Notifications.container);
                app.Pages.Notifications.instance.refresh();
            }
            app.changePage(app.Pages.Notifications, false, true);
        });

        Elements.setari.find('.main-meniu-link').html(translate('Module_Title_Settings'));
        Elements.setari.find("a:first").unbind().tap(function() {
            self.hide();
            if (app.CurrentPage.container.selector == app.Pages.Settings.container.selector)
                return history.back();
            if (!app.Pages.Settings.instance)
                app.Pages.Settings.instance = new SettingsPage(app.Pages.Settings.container);
            app.changePage(app.Pages.Settings, false, true);
        });

        var authData = app.getAuthenticationData();
        if (authData && authData.Companies && authData.Companies.length > 1) {
            Elements.detaliiFirma.show().tap(function() {
                if (!app.Pages.CompanyConnection.instance)
                    app.Pages.CompanyConnection.instance = new CompanyConnectionPage(app.Pages.CompanyConnection.container, authData.Companies, {
                        username: authData.Username,
                        password: authData.Password,
                        server: authData.Server
                    });

                app.changePage(app.Pages.CompanyConnection, false, true);
            });
        } else Elements.detaliiFirma.hide();

        Elements.deconectare.find('.main-meniu-link').html(translate('MyProfile_DisconnectBtn_Value'));
        Elements.deconectare.find("a:first").unbind().tap(function() {
            app.Phone.hideMenu();
            self.hide();
            app.disconnect();
        });

        Elements.overlay.unbind().bind('touchstart', function(e) {
            return Event.stopEvent(e);
        }).tap(function(e) {
            if (visible) {
                self.hide();
                return Event.stopEvent(e);
            }
            return Event.stopEvent(e);
        });

        container.unbind().bind('touchstart', function(e) {
            return Event.stopEvent(e);
        });

        container.click(function(e) {
            if (visible)
                e.stopPropagation();
        });

        $(document).bind("setUserPic", function(e) {
            Elements.pozaAngajat.attr("src", e.Data.src);
        });

        $(document).bind('updateUnreadNotifications', function(e) {
            Elements.numarNotificari.html('(' + e.Data.Nr + ')');
        });

        $(document).bind('changedCompany', function() {
            gotNotifications = false;
            updateUserPicture();
            updateCompany();
            setListaAngajatiMenu();
            setCereriConcediiMenu();
        });
    };

    this.isVisible = function() {
        return visible;
    };

    this.setPozaAngajat = function(src) {
        Elements.pozaAngajat.attr("src", src);
    };

    this.show = function() {
        if (app.User.Rights.ALL || app.User.Rights.AccesModulCereriConcedii)
            Elements.cereriConcediu.show();
        else Elements.cereriConcediu.hide();

        if (!gotNotifications) {
            app.updateUnreadNotifications();
            gotNotifications = true;
        }

        container.css('left', '0').css('top', $(document).scrollTop() + 'px');
        app.CurrentPage.container.css('left', container.outerWidth());
        Elements.overlay.css('height', $(document).height() + 'px').show();
        Elements.overlay.css('left', container.outerWidth() + 'px');
        Elements.overlay.css('width', ($(document).width() - container.outerWidth()) + 'px');
        visible = true;

        if (app.MessagesFactory.isDialogVisible())
            app.MessagesFactory.hideDialog();

        $(document).trigger('menu_opened');

        self.IsGoing = true;
    };

    this.hide = function() {
        if (visible) {
            container.css('left', '-' + (container.outerWidth() + 20) + 'px');
            app.CurrentPage.container.css('left', '0');
            Elements.overlay.css({
                'left': '0px',
                'width': '100%'
            });
            setTimeout(function() {
                Elements.overlay.hide();
            }, 500);
            visible = false;
        }
    };

    initialize();
};